forward_static_call
调用静态方法
forward_static_call
PHP 5.3.0 及以上版本
forward_static_call 用于在静态上下文中调用一个用户函数,并保持当前类的上下文(特别是 late static binding)。它通常用于静态继承链中调用父类的静态方法,并保留调用类的信息。
mixed forward_static_call(callable $function, mixed ...$args)
返回调用函数的结果。
public static function test() {
forward_static_call([self::class, 'who']);
}
}
class B extends A {
public static function who() {
echo CLASS;
}
}
B::test(); // 输出 B
?>
在上面的例子中,类 B 继承自 A。虽然 A::test() 使用的是 self::class 来指定调用 who 方法,但由于 forward_static_call 保留了调用的类上下文,因此实际输出的是类 B 的名称。这体现了 late static binding 的作用。